package com.gc.repository.impl;

import com.gc.entity.Teacher;
import com.gc.entity.TextBook;
import com.gc.repository.TextBookRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class TextBookRepositoryImpl implements TextBookRepository {

        /**
         *      Long id;
         *     String name;
         *     String ISBN;
         *     String publisher;
         *     String price;
         *
         * @param textBook
         * @return
         */
    @Override
    public boolean Add(TextBook textBook) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into textbook(bookid, name, ISBN, publisher, price) VALUES (?, ?, ?, ?, ?)",
                textBook.getBookid(),
                textBook.getName(),
                textBook.getISBN(),
                textBook.getPublisher(),
                textBook.getPrice()
            );
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<TextBook> FindAll() {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<TextBook> query = qr.query("select * from textbook", new BeanListHandler<>(TextBook.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean DelById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from textbook WHERE id = ?", id);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TextBook FindById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            TextBook res = qr.query("select * from textbook WHERE id = ?", new BeanHandler<>(TextBook.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public TextBook FindByBookId(String id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            TextBook res = qr.query("select * from textbook WHERE bookid = ?", new BeanHandler<>(TextBook.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean UpdateById(TextBook textBook) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update textbook set bookid = ?, `name` = ?, ISBN = ?, publisher = ?, price = ?  WHERE id = ?",
                    textBook.getBookid(),
                    textBook.getName(),
                    textBook.getISBN(),
                    textBook.getPublisher(),
                    textBook.getPrice(),
                    textBook.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
